﻿<%@ Page Title="Proposal Penelitian" Language="C#" MasterPageFile="~/Lab.master" AutoEventWireup="true" CodeFile="Peneliti_Proposal.aspx.cs" Inherits="Peneliti_Proposal" EnableViewState="false" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="BodyContent" Runat="Server">
    <div class="col-md-12 col-sm-12 col-xs-12">
        <div class="x_panel">
            <div class="x_title">
                <h2>Proposal Penelitian</h2>
                <ul class="nav navbar-right panel_toolbox">
                    <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                    </li>
                    <li class="dropdown">
                        <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                        <ul class="dropdown-menu" role="menu">
                            <li><a href="#">Settings 1</a>
                            </li>
                            <li><a href="#">Settings 2</a>
                            </li>
                        </ul>
                    </li>
                    <li><a class="close-link"><i class="fa fa-close"></i></a>
                    </li>
                </ul>
                <div class="clearfix"></div>
            </div>

            <div class="x_content">
                <form id="myForm" class="form-horizontal form-label-left" method="post" runat="server">
                    <div class="form-group">
                        <label class="control-label col-md-2 col-sm-3 col-xs-12" for="barang">TAHUN ANGGARAN<span class="required">*</span></label>
                        <div class="col-md-10 col-sm-3 col-xs-12">
                            <select class="select2_tahun form-control" tabindex="-1" name="tahun" id="tahun" required="required">
                                <asp:Repeater runat="server" ID="lstTahunAnggaran">
                                    <HeaderTemplate>
                                        <option></option>
                                    </HeaderTemplate>
                                    <ItemTemplate>
                                        <option value="<%# DataBinder.Eval(Container.DataItem, "TAHUN") %>"><%# DataBinder.Eval(Container.DataItem, "TAHUN") %></option>
                                    </ItemTemplate>
                                </asp:Repeater>
                              </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2 col-sm-3 col-xs-12">Kode Proposal<span class="required">*</span></label>
                        <div class="col-md-10 col-sm-3 col-xs-12">
                            <input type="text" id="kodeproposal" name="kodeproposal" required="required" class="form-control col-md-7 col-xs-12" placeholder="contoh : 1800.045.123 A">
                        </div>
                    </div>

                    <div class="form-group">
                        <label class="control-label col-md-2">Daftar Bahan Kimia</label>
                        <div class="col-md-10">
                            <table>
                                <tbody>
                                    <tr>
                                        <td>
                                            <div>
                                                <select class="form-control barang" tabindex="-1" name="barang[]" id="barang[]" required="required">
                                                    <asp:Repeater runat="server" ID="lstBarang">
                                                        <HeaderTemplate>
                                                            <option></option>
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <option value="<%# DataBinder.Eval(Container.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "NAMA_BARANG") %></option>
                                                        </ItemTemplate>
                                                    </asp:Repeater>
                                                  </select>
                                            </div>
                                        </td>
                                        <td><input class="form-control jumlah" name="jumlah[]" type="number" min="0" step="0.001" placeholder="Jumlah" required="required"/></td>
                                        <td style="white-space:nowrap">&nbsp;<label class="unit">--</label>&nbsp;</td>
                                        <td><input class="form-control" name="keterangan[]" type="text" placeholder="Keterangan" /></td>
                                        <td style="white-space:nowrap">&nbsp;
                                            <button class="btn btn-success btn-add" type="button" style="height:32px"><i class="glyphicon glyphicon-plus gs"></i></button>
                                            <button class="btn btn-danger btn-remove" type="button" style="height:32px"><i class="glyphicon glyphicon-minus gs"></i></button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>

                     <div class="form-group">
                        <label class="control-label col-md-2">Daftar Bahan Penunjang</label>
                        <div class="col-md-10">
                            <table>
                                <tbody>
                                    <tr>
                                        <td>
                                            <div>
                                                <select class="form-control barang" tabindex="-1" name="barang[]" id="barang[]">
                                                    <asp:Repeater runat="server" ID="lstBarangenunjang">
                                                        <HeaderTemplate>
                                                            <option></option>
                                                        </HeaderTemplate>
                                                        <ItemTemplate>
                                                            <option value="<%# DataBinder.Eval(Container.DataItem, "ID") %>"><%# DataBinder.Eval(Container.DataItem, "NAMA_BARANG") %></option>
                                                        </ItemTemplate>
                                                    </asp:Repeater>
                                                  </select>
                                            </div>
                                        </td>
                                        <td><input class="form-control jumlah" name="jumlah[]" type="number" min="0" step="0.001" placeholder="Jumlah"/></td>
                                        <td style="white-space:nowrap">&nbsp;<label class="unit">--</label>&nbsp;</td>
                                        <td><input class="form-control" name="keterangan[]" type="text" placeholder="Keterangan" /></td>
                                        <td style="white-space:nowrap">&nbsp;
                                            <button class="btn btn-success btn-add" type="button" style="height:32px"><i class="glyphicon glyphicon-plus gs"></i></button>
                                            <button class="btn btn-danger btn-remove" type="button" style="height:32px"><i class="glyphicon glyphicon-minus gs"></i></button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-2">
                            <asp:Button runat="server" ID="btnSubmit" Text="Simpan &amp; Cetak" CssClass="btn btn-primary" />
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="BodyFooterContainer" Runat="Server">
    <script>
        $(function () {
            $('[id="barang[]"]').change(function () {
                changeUnit(this);
                var jumlah = $(this).parents('tr').find('.jumlah');
                updateRequired(jumlah)
            });
        });

        function updateRequired(objSelect)
        {
            $(objSelect).attr('required', 'required');
        }

        function changeUnit(objSelect) {
            
            if ($(objSelect).val() == '') {
                return;
            }

            //e.preventDefault();

            var aData = [];
            var unit = $(objSelect).val();
            var jsonData = JSON.stringify({ unit: unit });
            $.ajax({
                type: "POST",
                url: "Peneliti_Proposal.aspx/GetUnitString",
                data: jsonData,
                contentType: "application/json; charset=utf-8",
                dataType: "json", // dataType is json format
                success: OnSuccess,
                error: OnErrorCall
            });

            function OnSuccess(response) {
                $(objSelect).parents('tr').find('.unit').text(response.d);
            }
            function OnErrorCall(response) {
                new PNotify({
                    title: 'Gagal mengambil data unit barang',
                    text: response.statusText + ' (' + response.status + ') : ' + response.responseJSON.Message,
                    type: 'error',
                    styling: 'bootstrap3'
                });
            }
            
        }

        $(function () {
            $(document).on('click', '.btn-add', function (e) {
                var $myForm = $('#myForm')
                //if (!$myForm[0].checkValidity()) {
                //    $myForm.find(':submit').click();
                //    return;
                //}

                //e.preventDefault();

                var controlForm = $(this).closest('table'),
                    currentEntry = $(this).parents('tr:first'),
                    newEntry = $(currentEntry.clone()).appendTo(controlForm);

                newEntry.find('input').val('');
                //controlForm.find('tr:not(:last) .btn-add')
                //    .removeClass('btn-add').addClass('btn-remove')
                //    .removeClass('btn-success').addClass('btn-danger')
                //    .html('<span class="glyphicon glyphicon-minus gs"></span>');

                var barang = newEntry.find('.barang');
                var unit = newEntry.find('.unit');
                var jumlah = newEntry.find('.jumlah');
                $(unit).text('');
                $(barang).on("change", function (e) {
                    changeUnit(barang);
                    updateRequired(jumlah)
                });
                $(jumlah).removeAttr('required');

            }).on('click', '.btn-remove', function (e) {
                if ($(this).closest('table').find('tr').length > 1) {
                    $(this).parents('tr:first').remove();
                    e.preventDefault();
                    return false;
                }
            });
        });
    </script>
</asp:Content>

